package com.movie.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movie.datacomm.common.CommResp;
import com.movie.datacomm.model.Order;
import com.movie.datacomm.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    OrderService orderService;


    @RequestMapping(value = "/getCurrentPageOrder", method = RequestMethod.GET)
    @ResponseBody
    public CommResp getCurrentPageOrder(int currentPage, int pageSize, @Nullable String input){
        Page<Order> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Order> wrapper = new QueryWrapper<Order>();
        if(!StringUtils.isEmpty(input)){
            wrapper.like("order_name", input);
        }
        wrapper.orderByAsc("order_id");
        page = orderService.page(page, wrapper);
        return CommResp.okList(page.getRecords(), page.getTotal());
    }

    @RequestMapping(value = "/updateOrderInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp updateOrderInfo(@RequestBody Order order){
        boolean result = orderService.updateById(order);
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("更新失败");
        }
    }

    @RequestMapping(value = "/addOrderInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp addOrderInfo(@RequestBody Order order){

        boolean result = orderService.save(order);
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("操作失败");
        }
    }

    @RequestMapping(value = "/deleteCurrentOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommResp deleteOrderInfo(@RequestBody Order order){

        boolean result = orderService.removeById(order.getOrderId());
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("删除失败");
        }
    }


}
